<script>
    layui.use(['layer'], function () {
        function getFieldName(node) {
            return node.parents('[data-field-name]').data('field-name');
        }

        var lottosItemCurrentIndex = $('.lottos-item').length;

        $(document).on('click', '.lottos-action .add', function () {
            var self = $(this);
            var fieldName = getFieldName(self);
            var itemHtml =
                '<tr class="lottos-item">' +
                '<td>' +
                '<input autocomplete="off" name="' + fieldName + '[' + lottosItemCurrentIndex + '][name]" class="layui-input">' +
                '<div class="validate-msg-box" data-validate="' + fieldName + '.' + lottosItemCurrentIndex + '.name"></div>' +
                '</td>' +
                '<td>' +
                '<input autocomplete="off" name="' + fieldName + '[' + lottosItemCurrentIndex + '][number]" class="layui-input">' +
                '<div class="validate-msg-box" data-validate="' + fieldName + '.' + lottosItemCurrentIndex + '.number"></div>' +
                '</td>' +
                '<td>' +
                '<input autocomplete="off" name="' + fieldName + '[' + lottosItemCurrentIndex + '][desc]" class="layui-input">' +
                '<div class="validate-msg-box" data-validate="' + fieldName + '.' + lottosItemCurrentIndex + '.desc"></div>' +
                '</td>' +
                '<td class="lottos-action">' +
                '<div class="layui-btn-group">' +
                '<button type="button" class="layui-btn layui-btn-xs add">添加</button>' +
                '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger remove">删除</button>' +
                '</div>' +
                '</td>' +
                '</tr>';
            var item = self.parents('.lottos-item');
            item.after(itemHtml);
            lottosItemCurrentIndex++;
        });

        $(document).on('click', '.lottos-action .remove', function () {
            var self = $(this);
            if ($('.lottos-item').length == 1) {
                layer.msg('至少要有一个奖项');
                return;
            }

            self.parents('.lottos-item').remove();
        });
    });
</script>